#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests, json
import bs4
import time
import traceback

s = requests.Session()


def login(name, password):
    """login"""
    url = "https://accounts.douban.com/j/mobile/login/basic"
    request_headers = {"Referer": "https://accounts.douban.com/passport/login_popup?login_source=anony",
                       "User-Agent": "Mozilla/5.0"}
    post_data = {
        "ck": "",
        "password": password,
        "remember": "false",
        "ticket": "",
        "name": name
    }
    try:
        res = s.post(url, data=post_data)
        print(res.text)
    except Exception:
        print("登录失败")


def douban_spilder(start=0):
    """爬取影评"""
    print("爬取第[" + str(start) + "]页影评")
    url = "https://movie.douban.com/subject/3742360/comments?start=%d&limit=20&sort=new_score&status=P&comments_only=1" % start
    request_headers = {"Referer": url, "User-Agent": "Mozilla/5.0",
                       "Cookies": 'bid=E8pmvTiwT2k; ll="118282"; _vwo_uuid_v2=D244C3CA3B0517C32C747A4C8F37A16FB|123dedb77581a1968e94cfe8f4c18008; __yadk_uid=aka2q1fidGBKAyO04wGsMlLvFWgQ78qy; __gads=ID=b1b6191ed92c8d64:T=1557736400:S=ALNI_MZbTye9hnaL3F-cr3hYEHfpzhZxIg; trc_cookie_storage=taboola%2520global%253Auser-id%3Df84b7b5b-a1e0-40d0-bc38-ff2be9ae4f45-tuct3921929; douban-fav-remind=1; __utmc=30149280; __utmc=223695111; ct=y; __utmz=30149280.1571290680.13.12.utmcsr=accounts.douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/passport/login; __utmz=223695111.1571290688.9.9.utmcsr=douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/search; _pk_ref.100001.4cf6=%5B%22%22%2C%22%22%2C1571294249%2C%22https%3A%2F%2Fwww.douban.com%2Fsearch%3Fq%3D%25E8%25AE%25A9%25E5%25AD%2590%25E5%25BC%25B9%25E9%25A3%259E%22%5D; _pk_ses.100001.4cf6=*; __utma=30149280.330551804.1553851077.1571290680.1571294249.14; __utma=223695111.1980347840.1554104550.1571290688.1571294249.10; __utmb=223695111.0.10.1571294249; __utmb=30149280.4.9.1571297212891; ap_v=0,6.0; _pk_id.100001.4cf6=456e3f1356354a0f.1554104550.10.1571299770.1571290948.'}
    try:
        res = s.get(url=url, headers=request_headers)
        res.raise_for_status()
        # print(res.text)
        return res.text
    except Exception as e:
        traceback.print_exc()
        print("获取影评失败")
    return None


def parse_html():
    start = 0
    while True:
        time.sleep(2)
        comment = douban_spilder(start)
        if comment is None:
            print("爬取第[" + str(start) + "]页影评失败")
            return
        start += 20
        jsonC = json.loads(comment)
        # print(type(jsonC))
        # print(jsonC["html"])
        soup = bs4.BeautifulSoup(jsonC["html"], "html.parser")
        content = soup.find_all("span", class_="short")
        text = ""
        for item in content:
            text = text + item.get_text()
        print(text)
        write_file(text)


def write_file(text):
    with open('comments.txt', 'a+', encoding='utf-8') as f:
        f.writelines(text)


if __name__ == '__main__':
    # 替换为自己的用户名和密码
    login("123456", "123456")
    parse_html()
